<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control :title="['流行','新款','精选']"
                 @tabclick="tabclick" ref="tabcontrol" class='tab-control' v-show="isFixed"></tab-control>
    <scroll class="content" ref="scroll" :probe-type="3"
            :pull-up-load="true" @scroll="contentScroll"
             @pullingup='pullingup'>
      <HomeSwipers :banners="banners" @swiperimgload="swiperimgload"></HomeSwipers>
    <HomeRecommend :recommends="recommends"></HomeRecommend>
    <Feature></Feature>
    <tab-control :title="['流行','新款','精选']"
                 @tabclick="tabclick" ref="tab" ></tab-control>
    <GoodList :goods="showGoods"></GoodList>
    </scroll>
    <back-top @click.native="topclick" v-show="isShow"/>
  </div>
</template>

<script>
  import NavBar from "@/components/common/navbar/NavBar";
  import TabControl from "@/components/content/tabcontrol/TabControl";
  import GoodList from "@/components/content/goods/GoodList";
  import scroll from "@/components/common/scroll/scroll";
  import BackTop from "@/components/content/backtop/BackTop";

  import HomeSwipers from "@/views/home/childComps/HomeSwipers";
  import HomeRecommend from "@/views/home/childComps/HomeRecommend";
  import Feature from "@/views/home/childComps/Feature";

   import {getHomeMultidata} from "@/network/home";
   import {getHomeGoods} from "@/network/home";
  import {mymixin} from "@/common/mixin";


  export default {
    name: "Home",
    components: {
      HomeRecommend,
      HomeSwipers,
      Feature,
      NavBar,
      TabControl,
      GoodList,
      scroll,
      BackTop
    },
    mixins:[mymixin],
    data() {
      return {
        banners: [],
        recommends: [],
        good:{
          'pop':{'page':0,'list':[]},
          'new':{'page':0,'list':[]},
          'sell':{'page':0,'list':[]}
        },
        currentType:'pop',
        isShow:false,
        isFixed:false,
        positiony:0,
        posY:0,
      }
    },
    created() {
     this.getHomeMultidata()
      this.getHomeGoods('new')
       this.getHomeGoods('pop')
       this.getHomeGoods('sell')

    },
    activated() {
     this.$refs.scroll.scrollTo(0,this.posY,0)
      this.$refs.scroll.refresh()
    },
    deactivated() {
      this.$bus.$off('aaa',this.itemimglistener)
       this.posY=this.$refs.scroll.getscrolly()
    },
    mounted() {
        // this.$refs.scroll&&this.$refs.scroll.refresh()

      //   const refresh=debounce(  this.$refs.scroll&&this.$refs.scroll.refresh,200)
      // this.itemimglistener=()=>{refresh()}
      // this.$bus.$on('aaa',this.itemimglistener)
    },
    computed:{
      showGoods(){
        return this.good[this.currentType].list
      }
    },
    methods:{

      // 这是事件监听模块
      //1.这是防抖动

      tabclick(index){
       switch (index){
         case 0:
           this.currentType='pop'
           break
         case 1:
           this.currentType='new'
           break
         case 2:
           this.currentType='sell'
           break
       }
       this.$refs.tabcontrol.controlindex=index
        this.$refs.tab.controlindex=index

      },
      topclick(){
        this.$refs.scroll.scrollTo(0,0,2000)
      },
      contentScroll(position){
        this.isShow=-position.y > 1000
        if(-position.y>this.$refs.tab.$el.offsetTop){
          this.isFixed=true;
        }else {this.isFixed=false}
        },
      pullingup(){
        this.getHomeGoods(this.currentType)
      },
       /*
    *这是网络请求模块
     */
      getHomeMultidata(){
         getHomeMultidata().then(
        res=>{
          this.banners=res.data.banner.list
          this.recommends=res.data.recommend.list
        }
      )
      },
      getHomeGoods(type){
        const page=this.good[type].page+1
          getHomeGoods(type,page).then(
            res=>{this.good[type].list.push(...res.data.list);
            this.good[type].page++;
            this.$refs.scroll.finishPullUp()}
          )
      },
      swiperimgload(){
        this.positiony=this.$refs.tab.$el.offsetTop
      }},
  }
</script>

<style scoped>
.home-nav{
  color: var(--color-background);
   background-color: var(--color-tint);
  position: relative;
  top:0;
  /*left: 0;*/
  /*right: 0;*/
  z-index: 9;
  /*padding-bottom: 44px;*/
  }
#home{
  /*padding-top: 44px;*/
  height: 100vh;
  position: relative;
}
  .tab-control {
    position: relative;
    z-index: 9;
  }
  .content{
   /*overflow: hidden;*/
    position: absolute;
    top:44px;
    bottom: 49px;
    left: 0;
    right: 0;
  }
</style>
